<?php


/*
 * @license   GPLv3 Open Source
 */


defined ('IN_AGORA') or die;

class TaskController extends AgoraAdminController
{

	function loadModel() {
		$this->model = Model::getInstance('JoopquestionsModel');
	}	

 	function _default()
	{
		$this->view->template = 'joopport/qkit';
		$this->setPagination($this->model->loadAllCountKit());
		$qkits = $this->model->get_kits('',$this->pagination->per_page,$this->pagination->page);

		$this->view->assignRef('qkits',$qkits);
	
	}

	function remove() {

		$cid = Agora::getPostVar('cid');

		foreach ($cid as $key=>$id ) {
			$group_name = $this->model->delete_kit($id);

		}	
		
		$this->model->clearforums();

		
		$this->redirect();
	}
	
	function add_kit() {
		$quests = $this->model->loadAll();
		$this->view->assign('quests',$quests);
		$this->view->template = 'joopport/add_kit';
	}
	
	function save() {
		$form = Agora::getPostVar('form');

		if($form['quest_id'] < 1 ) //Если вопрос не выбран
		{
			Agora::showError("Please select question");
			$this->redirect();
		}
		
		if (isset($form['kit_name'])) { //если существует kit_name, значит мы выполняем привязку существующей группы с вопросом
			if (!$this->model->kit_exists($form['kit_name'], $form['quest_id'])){
				$this->model->save_new_kit($form['quest_id'], $form['kit_name']);
			}
			else {
				Agora::showError("Such kit and question pare already exists"); 
				$this->redirect();
			}

		}
		else { //иначе (если kit_name не обнаружен) мы создаем новую группу вопросов

			if (!$this->model->kit_exists($form['group_name'])){
			//проверка существования выбранной группы
				$this->model->save_new_kit($form['quest_id'], $form['group_name']);
			}
			else {
				Agora::showError("Such kit already exists");
			}
		}
		$this->redirect();
	}
	
	function cancel() {
		$this->redirect();
	}
	
	function add_questkit() {
		$quests = $this->model->loadAll();
		$this->view->assign('quests',$quests);
		
		$knames = $this->model->get_kit_names();
		$this->view->assign('knames',$knames);
		
		
		$this->view->template = 'joopport/add_questkit';
	}

	
}